package com.wngbms.websocket.zookeeper;

import com.wngbms.websocket.support.StringUtil;
import org.apache.curator.framework.recipes.cache.ChildData;

import java.time.LocalDateTime;

public interface ZkConst {
    String SUCCESS = "success";

    String FAILED = "failed";

    String SERVICE_UUID = StringUtil.uuid();

    String NODE_ROOT_PATH = "/rx-seat";

    String TRIM_NODE_ROOT_PATH = NODE_ROOT_PATH + "/";

    String USER_KEY_PREFIX = "seatuser:";

    String HANDLE_EVENT_PREFIX = "HANDLE:EVENT:";

    /**
     * 心跳阈值2分钟
     */
    int HEARTBEAT_THRESHOLD = 120 * 1000;

    default String getContent(ChildData childData) {
        byte[] data = childData.getData();
        return new String(data, ZookeeperSupport.CHARSET);
    }

    default String getServiceRootPath() {
        return TRIM_NODE_ROOT_PATH + SERVICE_UUID;
    }

    default String getServiceRootPath(String serviceId) {
        return TRIM_NODE_ROOT_PATH + serviceId;
    }

    default String getUserServiceRootPath(String userId, String eventId, String serviceId) {
        return getServiceRootPath(serviceId) + "/" + userId + "--" + eventId;
    }

    default String getPushEventContent(String eventId, String userId) {
        return HANDLE_EVENT_PREFIX + eventId + "--" + userId;
    }

    default String getEventId(String content) {
        return content.substring(content.lastIndexOf(":") + 1).split("--")[0];
    }

    default String getEventUserId(String content) {
        return content.substring(content.lastIndexOf(":") + 1).split("--")[1];
    }

    default String getUserKey(String userId) {
        return USER_KEY_PREFIX + userId;
    }

}
